package com.example.tally;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.tally.adapter.AccountAdapter;
import com.example.tally.db.AccountBean;
import com.example.tally.db.DBManager;
import com.example.tally.utils.CalendarDialog;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends AppCompatActivity implements View.OnClickListener {
    EditText searchEt;
    ListView searchLv;
    TextView emptyTv;
    AccountAdapter adapter;
    List<AccountBean> mData;// 数据源

    /**记录点击之后年份和月份的位置，否则每次返回再打开的时候总是刚开始的位置*/
    int dialogSelectPos = -1;
    int dialogSelectMonth = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        init();
        mData = new ArrayList<>();
        adapter = new AccountAdapter(this , mData);
        searchLv.setAdapter(adapter);
        searchLv.setEmptyView(emptyTv); // 当无数据源的时候显示的控件
    }

    private void init() {
        searchEt = findViewById(R.id.search_et);
        searchLv =findViewById(R.id.search_lv);
        emptyTv = findViewById(R.id.search_tv_empty);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_iv_back:
                finish();
                break;
            case R.id.search_iv_sh:
                String mgs = searchEt.getText().toString().trim();
                // 判断输入内容是否为空，如果为空，则不显示内容
                if (TextUtils.isEmpty(mgs)) {
                    Toast.makeText(this , "输入内容不能为空" , Toast.LENGTH_SHORT).show();
                    return;
                }
                // 开始搜索
                List<AccountBean> beans = DBManager.getAccountListByRemarkFromAccounttb(mgs);
                mData.clear();
                mData.addAll(beans);
                adapter.notifyDataSetChanged();
                break;
            case R.id.search_calendar:
                CalendarDialog dialog = new CalendarDialog(this , dialogSelectPos , dialogSelectMonth);
                dialog.show();
                dialog.setDialogSize();
                dialog.setOnRefreshListener(new CalendarDialog.OnRefreshListener() {
                    @Override
                    public void Ensure(int selPos, int year, int month) {
                        // 开始搜索
                        String mgs = searchEt.getText().toString().trim();
                        // 判断输入内容是否为空，如果为空，则不显示内容
                        if (TextUtils.isEmpty(mgs)) {
                            Toast.makeText(SearchActivity.this , "输入内容不能为空" , Toast.LENGTH_SHORT).show();
                            return;
                        }
                        List<AccountBean> beans = DBManager.getAccountListByRemarkFromAccounttbYearMonth(mgs,year,month);
                        mData.clear();
                        mData.addAll(beans);
                        adapter.notifyDataSetChanged();
                        dialogSelectPos = selPos;
                        dialogSelectMonth = month;
                    }
                });
        }
    }
}